import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_09_game/base/controller/base_controller.dart';
import 'package:flutter_09_game/config/app_color.dart';
import 'package:flutter_09_game/config/app_routes.dart';
import 'package:flutter_09_game/generated/assets.dart';
import 'package:flutter_09_game/generated/locales.g.dart';
import 'package:flutter_09_game/http/http_export.dart';
import 'package:flutter_09_game/pages/profile/models/model_system_avatar.dart';
import 'package:flutter_09_game/user/app_user_manager.dart';
import 'package:flutter_09_game/utils/loading/loading_utils.dart';
import 'package:flutter_09_game/widgets/dialog/common_normal_sheet.dart';
import 'package:flutter_09_game/widgets/image/auto_image.dart';
import 'package:flutter_09_game/widgets/image/image_view.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class ProfileEditLogic extends BaseController {
  List<ModelSystemAvatar> systemAvatars = [];
  RxString avatarPath = RxString(AppUserManager().user.value?.avatar ??'');
  /// 跳转到编辑用户名页面
  void goToEditUserName() {
    Get.toNamed(AppRoutes.profileEditNamePage);
  }

  Future getSystemAvatar() async {
    HttpResponse response = await HttpManager().get(API.getAvatar,
        uiType: HttpUIType.loadingAndToast,
        fromJson: ModelSystemAvatar.fromJson);
    if (response.isSuccess) {
      systemAvatars = response.data;
    }
  }

  showAvatarSheet() async {
    if (systemAvatars.isEmpty) {
      await getSystemAvatar();
    }
    CommonNormalSheet(
      title: LocaleKeys.changeAvatar.tr,
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 24.w),
        margin: EdgeInsets.only(bottom: 16.w + ScreenUtil().bottomBarHeight),
        child: Obx(() {
          return Wrap(
            spacing: 21.w,
            runSpacing: 16.w,
            alignment: WrapAlignment.start,
            children: systemAvatars.map((item) {
              bool isSelect = item.image == avatarPath.value;
              return GestureDetector(
                onTap: () async {
                  avatarPath.value = item.image ??'';
                  // 调用API保存用户名
                  final response = await AppUserManager.updateUserInfoToServer(
                    avatar: item.image,
                  );
                  if (response.isSuccess) {
                    LoadingUtils.showToast(LocaleKeys.setSuccess.tr);
                    // 返回上一页
                    Get.back();
                  }
                },
                child: SizedBox(
                  width: 60.w,
                  height: 60.w,
                  child: Stack(
                    children: [
                      Positioned.fill(child: ImageView(
                        imageUrl: item.image,
                        radius: 30.w,
                        border: Border.all(
                            color:isSelect ?  AppColor.primary : Colors.transparent,
                            width: 2
                        ),
                      )),
                      Visibility(
                          visible: isSelect,
                          child:  Container(
                            alignment: Alignment.center,
                            // color: Colors.red,
                            child: AutoImage(Assets.commonSelectMine,width: 24.w,),
                          ))
                    ],
                  ),
                ),
              );
            }).toList(),
          );
        }),
      ),
    ).show();
  }
}
